/*
 * Filename:    CaseCommonServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014-11-21
 */
package com.paic.mhis.hcpms.audit.biz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.core.auth.biz.service.UserDetails;
import com.paic.mhis.hcpms.audit.biz.service.ICaseCommonService;
import com.paic.mhis.hcpms.audit.constants.AuditConstants;
import com.paic.mhis.hcpms.audit.dao.AuditOpinionDao;
import com.paic.mhis.hcpms.audit.dao.AuditSurveyReviewDao;
import com.paic.mhis.hcpms.audit.dao.CaseCommonDao;
import com.paic.mhis.hcpms.audit.dto.AuditInsuranceInfoDTO;
import com.paic.mhis.hcpms.audit.dto.AuditMedicalInfoDTO;
import com.paic.mhis.hcpms.audit.dto.AuditOpinionDTO;
import com.paic.mhis.hcpms.audit.dto.AuditPointCutTempDTO;
import com.paic.mhis.hcpms.audit.dto.AuditTargetDTO;
import com.paic.mhis.hcpms.audit.dto.AuditiStaffInfoDTO;
import com.paic.mhis.hcpms.audit.dto.CaseCommonRequestDTO;
import com.paic.mhis.hcpms.audit.dto.CommonInsureanceDTO;
import com.paic.mhis.hcpms.audit.dto.CommonMedicalDTO;
import com.paic.mhis.hcpms.audit.dto.CommonStaffDTO;
import com.paic.mhis.hcpms.common.storage.biz.service.HcpmsStorageService;
import com.paic.mhis.hcpms.common.storage.dto.HcpmsStorageDTO;
import com.paic.mock.util.StringUtils;

@Component("caseCommonServiceImpl")
public class CaseCommonServiceImpl implements ICaseCommonService {

	@Autowired
	private CaseCommonDao caseCommonDao;

	@Autowired
	private AuditSurveyReviewDao auditDao;

	@Autowired
	private AuditOpinionDao auditOpinionDao;

	@Autowired
	private HcpmsStorageService hcpmsStorageService;

	public CommonMedicalDTO queryCommonMedicalBean(AuditMedicalInfoDTO bean) {

		String medicalName = bean.getMedicalName();
		String medicalCode = bean.getMedicalCode();
		if (StringUtils.hasText(medicalName)) {
			bean.setMedicalName("'%" + medicalName + "%'");
		}
		if (StringUtils.hasText(medicalCode)) {
			bean.setMedicalCode("'%" + medicalCode + "%'");
		}

		List<AuditMedicalInfoDTO> list = this.caseCommonDao
				.queryAuditMedicalInfoDTO(bean);
		long count = this.caseCommonDao.queryAuditMedicalInfoDTOCount(bean);

		CommonMedicalDTO returnBean = new CommonMedicalDTO();

		returnBean.setMedicalCount(count);
		returnBean.setMedicalList(list);

		return returnBean;
	}

	public CommonStaffDTO queryCommonStaffBean(AuditiStaffInfoDTO bean) {

		String name = bean.getName();
		String medicalName = bean.getMedicalName();
		String idCard = bean.getIdCard();

		if (StringUtils.hasText(name)) {
			bean.setName("'%" + name + "%'");
		}
		if (StringUtils.hasText(medicalName)) {
			bean.setMedicalName("'%" + medicalName + "%'");
		}
		if (StringUtils.hasText(idCard)) {
			bean.setIdCard("'%" + idCard + "%'");
		}

		List<AuditiStaffInfoDTO> list = this.caseCommonDao
				.queryAuditiStaffInfoDTO(bean);
		long count = this.caseCommonDao.queryAuditiStaffInfoDTOCount(bean);

		CommonStaffDTO returnBean = new CommonStaffDTO();

		returnBean.setStaffCount(count);
		returnBean.setStaffList(list);

		return returnBean;

	}

	public CommonInsureanceDTO queryCommonInsureanceBean(
			AuditInsuranceInfoDTO bean) {

		String name = bean.getName();
		String insuranceNo = bean.getInsuranceNo();
		String idCard = bean.getIdCard();

		if (StringUtils.hasText(name)) {
			bean.setName("'%" + name + "%'");
		}

		if (StringUtils.hasText(insuranceNo)) {
			bean.setInsuranceNo("'%" + insuranceNo + "%'");
		}

		if (StringUtils.hasText(idCard)) {
			bean.setIdCard("'%" + idCard + "%'");
		}

		List<AuditInsuranceInfoDTO> list = this.caseCommonDao
				.queryAuditInsuranceInfoDTO(bean);
		long count = this.caseCommonDao.queryAuditInsuranceInfoDTOCount(bean);

		CommonInsureanceDTO returnBean = new CommonInsureanceDTO();

		returnBean.setInsuranceCount(count);
		returnBean.setInsuranceList(list);
		return returnBean;
	}

	@Transactional
	@Override
	public void deleteAuditTargetInfo(AuditTargetDTO info) {
		this.caseCommonDao.deleteAuditTargetInfo(info);

		AuditPointCutTempDTO point = new AuditPointCutTempDTO();

		String taskCode = info.getTaskCode();
		String targetCode = info.getTargetCode();
		String targetType = info.getTargetType();

		point.setCaseNo(taskCode);
		point.setCaseType("02");
		point.setTargetNo(targetCode);

		if (AuditConstants.targetType_02.equals(targetType)) {// 医生
			point.setTargetType("02");
		}

		if (AuditConstants.targetType_03.equals(targetType)) {// 参保人
			point.setTargetType("03");
		}

		this.caseCommonDao.deleteAuditPointCutTempDTO(point);

	}

	@Override
	public List<AuditPointCutTempDTO> loadAuditPointCutTemp(
			AuditPointCutTempDTO dto) {
		return caseCommonDao.loadAuditPointCutTempDTO(dto);
	}

	@Override
	public AuditOpinionDTO queryDispatchInfo(String taskCode, String taskType,
			String opinionType) {
		AuditOpinionDTO parameter = new AuditOpinionDTO();
		parameter.setTaskType(taskType);
		parameter.setProblemNo(taskCode);
		parameter.setOpinionType(opinionType);
		parameter.setIsDeleted(AuditConstants.isDeleted);

		List<AuditOpinionDTO> returnBeanList = this.auditOpinionDao
				.queryAuditOpinionDTOInfo(parameter);

		if (returnBeanList != null && returnBeanList.size() > 0) {

			return returnBeanList.get(0);
		} else {
			return null;
		}

	}

	/**
	 * 将参保人和医护人员临时表信息转移到正式表中，并删除临时表信息
	 */
	@Transactional
	@Override
	public void saveMedicalCommonInfo(CaseCommonRequestDTO vo, UserDetails user) {

		List<AuditTargetDTO> beanList = new ArrayList<AuditTargetDTO>();

		List<Object> list = vo.getList();

		for (int i = 0; i < list.size(); i++) {
			String currentStr = list.get(i).toString();
			JSONObject transferJson = JSON.parseObject(currentStr);

			AuditTargetDTO bean = JSONObject.toJavaObject(transferJson,
					AuditTargetDTO.class);
			if (bean != null && StringUtils.hasText(bean.getTaskCode())) {
				beanList.add(bean);
			}
		}

		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();
		if (vo.getTaskType() != null
				&& vo.getTaskType().equals(AuditConstants.taskType_01)) {
			storageBean.setModuleType(AuditConstants.sjModule);
		} else if (vo.getTaskType() != null
				&& vo.getTaskType().equals(AuditConstants.taskType_02)) {
			storageBean.setModuleType(AuditConstants.jhModule);
		}
		storageBean.setChildModuleType(AuditConstants.medicalCommon);
		storageBean.setBusinessType(vo.getTargetType());
		storageBean.setBusinessId(vo.getTaskCode());
		storageBean.setUserId(user.getUsername());
		this.hcpmsStorageService.deleteStorageData(storageBean);

		// 清空正式biao
		AuditTargetDTO targetBean = new AuditTargetDTO();
		targetBean.setTaskCode(vo.getTaskCode());
		targetBean.setTaskType(vo.getTaskType());
		targetBean.setTargetType(vo.getTargetType());
		this.auditDao.deleteAuditTarget(targetBean);

		if (beanList != null && beanList.size() > 0) {
			for (AuditTargetDTO currentBean : beanList) {
				currentBean.setIsDeleted(AuditConstants.isDeleted);
				this.auditDao.insertAuditTargetDTO(currentBean);
			}
		}

	}

	/**
	 * 暂存医护人员和参保人员信息到临时苦中
	 */
	@Transactional
	@Override
	public void saveAuditTargetInfo(CaseCommonRequestDTO vo, UserDetails user) {

		List<AuditTargetDTO> beanList = new ArrayList<AuditTargetDTO>();
		List<Object> list = vo.getList();

		for (int i = 0; i < list.size(); i++) {
			String currentStr = list.get(i).toString();
			JSONObject transferJson = JSON.parseObject(currentStr);

			AuditTargetDTO bean = JSONObject.toJavaObject(transferJson,
					AuditTargetDTO.class);
			if (bean != null && StringUtils.hasText(bean.getTaskCode())) {
				beanList.add(bean);
			}

		}

		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();
		if (vo.getTaskType() != null
				&& vo.getTaskType().equals(AuditConstants.taskType_01)) {
			storageBean.setModuleType(AuditConstants.sjModule);
		} else if (vo.getTaskType() != null
				&& vo.getTaskType().equals(AuditConstants.taskType_02)) {
			storageBean.setModuleType(AuditConstants.jhModule);
		}
		storageBean.setChildModuleType(AuditConstants.medicalCommon);
		storageBean.setBusinessType(vo.getTargetType());
		storageBean.setBusinessId(vo.getTaskCode());
		storageBean.setUserId(user.getUsername());
		storageBean.setDataMap(beanList);
		this.hcpmsStorageService.saveStorageData(storageBean, false);
	}

	/**
	 * 将任务号和用户名称,扣分json数据保存到临时表中
	 * 
	 * @param taskCode
	 * @param pointJson
	 * @param username
	 */
	@Transactional
	@Override
	public void saveAuditCasePoint(String taskCode, JSONObject pointJson,
			String username) {
		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();
		storageBean.setBusinessId(taskCode);
		storageBean.setModuleType(AuditConstants.MODULE_TYPE_02);
		storageBean.setBusinessType(AuditConstants.BUSINESS_TYPE_POINT);
		storageBean.setDataMap(pointJson);
		storageBean.setUserId(username);
		this.hcpmsStorageService.saveStorageData(storageBean, false);

	}

	/**
	 * 稽核部分对扣分删除的处理
	 */
	@Transactional
	@Override
	public void deleteAuditCasePoint(String taskCode, String username) {
		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();
		storageBean.setBusinessId(taskCode);
		storageBean.setModuleType(AuditConstants.MODULE_TYPE_02);
		storageBean.setBusinessType(AuditConstants.BUSINESS_TYPE_POINT);
		storageBean.setUserId(username);
		this.hcpmsStorageService.deleteStorageData(storageBean);

	}

	/**
	 * 批量操作参保人和医护人员的公共部分
	 * 
	 * @param taskCode
	 * @param taskType
	 *            其中taskType :AuditConstants.taskType_01 事件
	 *            AuditConstants.taskType_02 案件
	 */
	@Transactional
	@Override
	public void batchOpertionMedicalCommon(String taskCode, String taskType,
			String username) {

		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();

		if (taskType != null && taskType.equals(AuditConstants.taskType_01)) {
			storageBean.setModuleType(AuditConstants.sjModule);
		} else if (taskType != null
				&& taskType.equals(AuditConstants.taskType_02)) {
			storageBean.setModuleType(AuditConstants.jhModule);
		}
		storageBean.setChildModuleType(AuditConstants.medicalCommon);
		storageBean.setBusinessId(taskCode);
		storageBean.setUserId(username);

		HcpmsStorageDTO copyBean = new HcpmsStorageDTO();
		BeanUtils.copyProperties(storageBean, copyBean);

		this.processBatchStaffInfo(storageBean, taskCode, taskType);
		this.processBatchInsuarnceInfo(storageBean, taskCode, taskType);
		this.hcpmsStorageService.deleteStorageData(copyBean);

	}

	/**
	 * 批量操作的时候对医务人员的临时表信息转移到正式表中的操作
	 * 
	 * @param storageBean
	 */
	@Transactional
	private void processBatchStaffInfo(HcpmsStorageDTO storageBean,
			String taskCode, String taskType) {
		List<AuditTargetDTO> list = null;
		storageBean.setBusinessType(AuditConstants.targetType_02);
		Object obj = this.hcpmsStorageService.getStorageDataMap(storageBean);
		if (obj != null) {
			JSONArray arrayList = JSONObject.toJavaObject(((JSONArray) obj),
					JSONArray.class);
			if (arrayList != null && arrayList.size() > 0) {

				list = new ArrayList<AuditTargetDTO>();

				for (Object currentObj : arrayList) {
					JSONObject transferJson = JSON.parseObject(currentObj
							.toString());
					AuditTargetDTO bean = JSONObject.toJavaObject(transferJson,
							AuditTargetDTO.class);
					list.add(bean);
				}

			} else if (arrayList != null && arrayList.size() == 0) {
				list = new ArrayList<AuditTargetDTO>();
			}
		}

		if (list != null) {

			// 清空正式biao
			AuditTargetDTO targetBean = new AuditTargetDTO();
			targetBean.setTaskCode(taskCode);
			targetBean.setTaskType(taskType);
			targetBean.setTargetType(AuditConstants.targetType_02);
			this.auditDao.deleteAuditTarget(targetBean);

			if (list.size() > 0) {
				for (AuditTargetDTO currentBean : list) {
					currentBean.setIsDeleted(AuditConstants.isDeleted);
					this.auditDao.insertAuditTargetDTO(currentBean);
				}

			}
		}
	}

	/**
	 * 批量操作的时候对参保人员的信息的临时表信息转移到正式表中的操作
	 * 
	 * @param storageBean
	 * @param taskCode
	 * @param taskType
	 */
	@Transactional
	private void processBatchInsuarnceInfo(HcpmsStorageDTO storageBean,
			String taskCode, String taskType) {
		List<AuditTargetDTO> list = null;
		storageBean.setBusinessType(AuditConstants.targetType_03);
		Object obj = this.hcpmsStorageService.getStorageDataMap(storageBean);
		if (obj != null) {
			JSONArray arrayList = JSONObject.toJavaObject(((JSONArray) obj),
					JSONArray.class);
			if (arrayList != null && arrayList.size() > 0) {

				list = new ArrayList<AuditTargetDTO>();

				for (Object currentObj : arrayList) {
					JSONObject transferJson = JSON.parseObject(currentObj
							.toString());
					AuditTargetDTO bean = JSONObject.toJavaObject(transferJson,
							AuditTargetDTO.class);
					list.add(bean);
				}

			} else if (arrayList != null && arrayList.size() == 0) {
				list = new ArrayList<AuditTargetDTO>();
			}
		}

		if (list != null) {

			// 清空正式biao
			AuditTargetDTO targetBean = new AuditTargetDTO();
			targetBean.setTaskCode(taskCode);
			targetBean.setTaskType(taskType);
			targetBean.setTargetType(AuditConstants.targetType_03);
			this.auditDao.deleteAuditTarget(targetBean);

			if (list.size() > 0) {
				for (AuditTargetDTO currentBean : list) {
					currentBean.setIsDeleted(AuditConstants.isDeleted);
					this.auditDao.insertAuditTargetDTO(currentBean);
				}

			}
		}
	}

	/**
	 * 根据任务号和用户名称从临时表中查询出扣分暂存的数据
	 * 
	 * @param taskCode
	 * @param username
	 * @return
	 */
	@Override
	public JSONObject queryBatchOpertionAuditCasePoint(String taskCode,
			String username) {
		JSONObject jsonObject = null;
		HcpmsStorageDTO storageBean = new HcpmsStorageDTO();
		storageBean.setBusinessId(taskCode);
		storageBean.setModuleType(AuditConstants.MODULE_TYPE_02);
		storageBean.setBusinessType(AuditConstants.BUSINESS_TYPE_POINT);
		storageBean.setUserId(username);

		Object obj = this.hcpmsStorageService.getStorageDataMap(storageBean);
		if (obj != null) {
			jsonObject = (JSONObject) obj;

		}
		return jsonObject;

	}

}
